package greendbgui

class PersonTagLib 
{
    static namespace = 'pe'

    def person =
    {
        attrs ->
        
        def personId = attrs['id']
        
//        out << "<p>Id: " + personId + "</p>"
                             
        Person p = Person.get(personId)
        
        out << "<p>(" + p.id + ") " + p.surname + ", " + p.firstNames
        
        if (p.birthday18 != null)
        {
            out << " (18th: " + p.birthday18.format ("dd MMM yyyy") + ")"
        }
        
        out << "</p>"
    }
    
    def addresses =
    {
        attrs ->
        
        def personId = attrs['id']
//        def entryId = attrs['entryId']
        
//        out << "<p>Id: " + personId + ". rollId: " + rollId + "</p>"
        
//        ElectoralRollEntry e = ElectoralRollEntry.get (entryId)
        
        Person p = Person.get(personId)
        
        String title = "Latest Address"
        
        def rollEntries = p.electoralRollEntry.sort {it -> it.electoralRoll.date}.reverse()
        
        Address currentAddress = rollEntries.get(0).address
        List<ElectoralRoll> currentRolls = new LinkedList<ElectoralRoll>()
        currentRolls.add (rollEntries.get(0).electoralRoll)
        
        rollEntries.remove(0)
        
        
        for (rollEntry in rollEntries)
        {
            if (rollEntry.address.equals (currentAddress))
            {
                currentRolls.add (rollEntry.electoralRoll)
            }
            else
            {
                outputCurrentAddress (title, currentAddress, currentRolls)
                
                title = "Previous Address"
                currentAddress = rollEntry.address
                currentRolls.add (rollEntry.electoralRoll)
            }
        }
        
        outputCurrentAddress (title, currentAddress, currentRolls)
    }
    
    def outputCurrentAddress (title, address, rolls)
    {
        out << "<h3>" << title << "</h3>"
        
        out << "<p>"
        for (line in address.getMultiLine())
        {
            out << line << "<br/>"
        }
        out << "</p>"
        
        out << "<p>"
        for (roll in rolls)
        {
            out << roll << "<br/>"
        }
        out << "</p>"
    }
    
    
    def voting_record =
    {
        attrs ->
        
        def personId = attrs['id']
        
//        out << "<p>Id: " + personId + ". rollId: " + rollId + "</p>"
        
        Person p = Person.get(personId)
        
        def votingRecord = p.votingInfo.sort {it -> it.election.date}.reverse()
        
        out << "<h3>Voting Record</h3>"
        
        if (votingRecord.size == 0)
        {
            out << "<p>None found</p>" 
        }
        else
        {
            out << "<table>"
            out << "<tr>"
            out << "<th>Date</th>"
            out << "<th>Vote Type</th>"
            out << "</tr>"
            
            for (vote in votingRecord)
            {
                out << "<tr>"
                out << "<td>"
                out << vote.election.date.format("MMM yy") << " (" << vote.election.type << ")"
                out << "</td>"
                out << "<td>"
                out << vote.voted
                out << "</td>"
                out << "</tr>"
            }
                    
            out << "</table>"
        }
    }
    
    def contacts =
    {
        attrs ->
        
        def personId = attrs['id']
        
//        out << "<p>Id: " + personId + ". rollId: " + rollId + "</p>"
        
        Person p = Person.get(personId)
        
        def contacts = p.contact.sort {it -> it.date}.reverse()
        
        out << "<h3>Contact</h3>"
        
        if (contacts.size == 0)
        {
            out << "<p>None found</p>" 
        }
        else
        {
            out << "<table>"
            out << "<tr>"
            out << "<th>Date</th>"
            out << "<th>Party</th>"
            out << "<th>Notes</th>"
            out << "</tr>"
            
            for (contact in contacts)
            {
                out << "<tr>"
                out << "<td>"
                out << contact.date.format("dd MMM yy")
                out << "</td>"
                out << "<td>"
                out << contact.party
                out << "</td>"
                out << "<td>"
                out << contact.notes
                out << "</td>"
                out << "</tr>"
            }
                    
            out << "</table>"
        }
    }

    def aliases =
    {
        attrs ->
        
        def personId = attrs['id']
        def entryId = attrs['entryId']
        
//        out << "<p>Id: " + personId + ". rollId: " + rollId + "</p>"
        
        Person p = Person.get(personId)
        
        List aliases = new LinkedList()
        aliases.addAll (0, p.match2.toArray())
        aliases.addAll (0, p.match1.toArray())
        
        
        if (aliases.size > 0)
        {
            out << "<h3>Aliases</h3>"
        
            out << "<table>"
            out << "<tr>"
            out << "<th>Alias</th>"
            out << "<th>Confirmed</th>"
            out << "<th>Notes</th>"
            out << "</tr>"
            
            for (alias in aliases)
            {
                out << "<tr>"
                out << "<td>"
                
                Person other = alias.person1
                
                if (other.id == p.id)
                    other = alias.person2
                    
                out << g.link (controller:"person", action:"display", params:[id:other.id, entryId:entryId]) {other}
                
                out << "</td>"
                out << "<td>"
                out << alias.confirmed
                out << "</td>"
                out << "<td>"
                out << alias.notes
                out << "</td>"
                out << "</tr>"
            }
                    
            out << "</table>"
        }
    }
}
